<?php
class Merchant_Model_Validate_CommonValidate{
	public $order_id;
    public $order_code;
    public $consignee_address1;
    public $consignee_address2;
    public $consignee_city;
    public $consignee_company;
    public $consignee_country_id;
    public $consignee_email;
    public $consignee_first_name;
    public $consignee_last_name;
    public $consignee_phone;
    public $consignee_region;
    public $consignee_zip;
    public $customer_order_code;
    public $parcel_contents;
    public $parcel_declared_value;
    public $parcel_quantity;
    public $remark;
    public $shipping_method;
    public $order_product;
    public $customer_id;
    public $_error;
    public $_lang;

    public function __construct($array){
        if($array!=null){
			foreach($array as $key=>$val){
				$this->set($key,$val);
			}
		}
        $this->_error =array();
        $this->getLang();
	}

    public function set($key,$val){
        $this->$key =$val;
	}

    public function getLang(){
        $this->_lang =Common_Service_Get::lang();
    }

	public function product(){
		$totalQuantity = 0;
		if(is_array($this->order_product)){
			foreach($this->order_product as $key=>$value){
				$totalQuantity += $value['op_quantity'];
			}
		}
		if($totalQuantity == 0){
			$this->_error[] = $this->_lang['PRODUCT'] .$this->_lang['REQUIRED'];
		}
	}
	
	public function customerOrderCode(){
		if(trim($this->customer_order_code) != ''){
			$customerId = Merchant_Service_Customer::getCustomerId($this->customer_id);
			$orderModel = new Merchant_Model_Orders();
			$order = $orderModel->getByCondition(array("customer_id" => $customerId,"customer_order_code" => $this->customer_order_code,"order_id" => $this->order_id,"order_status_arr"=>array(1,2,3,4)));
			if(count($order) > 0){
                $this->_error[] = $this->_lang['REFERENCE_NO'] .$this->_lang['EXISTS'];
			}
		}
	}
	
    public function address(){
        $regex[] = array('name' => $this->_lang['ADDRESS_ONE'],'value' => $this->consignee_address1,'regex' => array('require','length[1,35]','noCharacter'));
        $regex[] = array('name' => $this->_lang['ADDRESS_TWO'],'value' => $this->consignee_address2,'regex' => array('length[1,35]'));
		$error=Default_Service_Validator::formValidator($regex);
        if(!empty($error)){
			foreach ($error as $one){
			    $this->_error[] = $one ;
			}
		}
	}

    public function shippingMethod(){
        $regex = array('name' =>$this->_lang['SHIPPING_METHOD'],'value' =>$this->shipping_method,'regex' => array('require'));
		$error=Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}

    public function consigneeCountryId(){
        $regex = array('name' => $this->_lang['CONSIGNEE'].$this->_lang['COUNTRY'],'value' => $this->consignee_country_id,'regex' => array('require'));
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}

    public function consigneeName(){
        $regex[] = array('name' => $this->_lang['CONSIGNEE'].$this->_lang['FIRST_NAME'],'value' => $this->consignee_first_name .' '.$this->consignee_last_name,'regex' => array('require','length[1,35]','noCharacter'));
		$error=Default_Service_Validator::formValidator($regex);
		if(!empty($error)){
			foreach ($error as $one){
			    $this->_error[] = $one ;
			}
		}
	}
	
	//公司名称
	public function consigneeCompany(){
		$regex = array('name' =>$this->_lang['COMPANY'],'value' => $this->consignee_company,'regex' => array('require'));
		$error=Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}

	//物品详细描述
	public function orderProduct(){
        if(count($this->order_product)==0) {
			$this->_error[] = $this->_lang['PRODUCT'] .$this->_lang['REQUIRED'];
		}
		if(is_array($this->order_product)){
			foreach ($this->order_product as $key =>  $val){
                $key++;
				$worthRegex = array('require','positive');
				$regex[] =array('name' =>$this->_lang['QUANTITY'].'-'.$key,'value' => $val['op_quantity'],'regex' => array('require','positive','number[0,999999]'));
				$error=Default_Service_Validator::formValidator($regex);
				if(!empty($error)){
					foreach ($error as $one){
						$this->_error[] = $one ;
					}
				}
			}
		}
	}
	
	//收件人城市
	public function consigneeCity(){
        $regex = array('name' => $this->_lang['CONSIGNEE'].$this->_lang['CITY'],'value' => $this->consignee_city,'regex' => array('require'));
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
	//收件人州
	public function consigneeRegion(){
		$regex = array('name' =>$this->_lang['CONSIGNEE'].$this->_lang['STATE_PROVICE'],'value' => $this->consignee_region,'regex' => array('length[1,128]','noCharacter'));
        //日本,美国,澳大利亚，加拿大
        $countryIdArr = array(41,253,13,118);
        if(in_array($this->consignee_country_id,$countryIdArr)){
            $regex['regex'][] = "require";
        }
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
	//物品申报价值
	public function parcelDeclaredValue(){
		$regex = array('name' => $this->_lang['DECLARE_VALUE'],'value' => $this->parcel_declared_value,'regex' => array('positive','require'));
        $regex['regex'][] = 'number[0,999999]';
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
	//邮编
	public function consigneeZip(){
		$regex = array('name' => $this->_lang['POSTAL_CODE'],'value' => $this->consignee_zip,'regex' => array('length[1,255]','noCharacter','require'));
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
	//发往美国邮编5位
	public function usZip(){
		$regex = array('name' => $this->_lang['POSTAL_CODE'],'value' => $this->consignee_zip,'regex' => array('length[1,255]','noCharacter','require'));
		if($this->consignee_country_id==253)
        {
            $regex['regex'][] = 'us_zip';
			$error = Default_Service_Validator::Validator($regex);
			if(!empty($error)){
				$this->_error[] = $error ;
			}
		}
	}
	
	//电话
	public function consigneePhone(){
 		$regex = array('name' => $this->_lang['PHONE'],'value' => $this->consignee_phone,'regex' => array('length[1,30]','noCharacter','require'));
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
	//Email
	public function consigneeEmail(){
 		$regex = array('name' =>  'Email','value' => $this->consignee_email,'regex' => array('require','email'));
		$error = Default_Service_Validator::Validator($regex);
		if(!empty($error)){
			$this->_error[] = $error ;
		}
	}
	
    public function validator(){
		$this->PublicValidator();
        return $this->_error;
    }
	
	public function PublicValidator(){
        $this->shippingMethod();
        $this->consigneeName();
        $this->consigneeCountryId();
        $this->address();
        $this->product();
        $this->customerOrderCode();
		$this->usZip();
    }

}
?>